package com.easygem.front.service.imp;

import com.easygem.common.model.front.ResourceInfo;
import com.easygem.common.vo.front.ResourceInfoVO;
import com.easygem.common.vo.system.PageVO;
import com.easygem.front.mapper.CustomerMapper;
import com.easygem.front.service.CustomerService;
import com.easygem.front.service.base.AbstractServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 肖智
 * @version 1.0
 * @Description 客户业务逻辑层的实现
 * @date 2024/12/24
 */
@Service
public class CustomerServiceImpl extends AbstractServiceImpl<CustomerMapper, ResourceInfo, Integer> implements CustomerService {
    @Autowired
    CustomerMapper resourceInfoMapper;
    //当前登录用户

    //查询客户列表
    @Override
    public PageVO<ResourceInfo> findCustomerList(Integer pageNum, Integer pageSize, ResourceInfoVO resourceInfoVO) {
        PageHelper.startPage(pageNum, pageSize);
        ResourceInfo resourceInfo = new ResourceInfo();
        BeanUtils.copyProperties(resourceInfoVO, resourceInfo);
        List<ResourceInfo> resourceInfoList = resourceInfoMapper.selectCustomerList(resourceInfo);
        PageInfo<ResourceInfo> info = new PageInfo<>(resourceInfoList);
        return new PageVO<>(info.getTotal(), resourceInfoList);
    }

    //查询客户详情
    @Override
    public ResourceInfo getCustomerById(Long resourceId) {
        return resourceInfoMapper.getCustomerById(resourceId);
    }

    //编辑客户
    @Override
    public Integer editCustomerById(ResourceInfo resourceInfo) {
        return resourceInfoMapper.editCustomerById(resourceInfo);
    }

    //查询客户标签列表
    @Override
    public Integer addCustomerLabel(Long resourceId, Integer labelId) {
        return resourceInfoMapper.addCustomerLabel(resourceId, labelId);
    }

    //删除客户
    @Override
    public Integer deleteCustomerById(Long[] resourceIds, String deleteTime) {
        return resourceInfoMapper.deleteCustomerById(resourceIds, deleteTime);
    }

    //添加客户
    @Override
    public Integer addCustomer(ResourceInfo resourceInfo) {
        return resourceInfoMapper.addCustomer(resourceInfo);
    }

    //客户转移
    @Override
    public Integer clientShift(Long[] resourceIds, Long[] userIds) {
        // 检查资源和用户数量是否有效
        if (resourceIds == null || userIds == null || resourceIds.length == 0 || userIds.length == 0) {
            throw new IllegalArgumentException("资源列表或用户列表不能为空！");
        }
        int totalResources = resourceIds.length;
        int totalUsers = userIds.length;
        // 计算每个用户至少应该分配的资源数
        int baseResourceCount = totalResources / totalUsers;
        int remainingResources = totalResources % totalUsers;
        int updateCount = 0;
        int resourceIndex = 0;
        // 循环分配资源
        for (int i = 0; i < totalUsers; i++) {
            // 当前用户要分配的资源数量
            int resourcesForUser = baseResourceCount + (i < remainingResources ? 1 : 0);
            // 给当前用户分配资源
            for (int j = 0; j < resourcesForUser; j++) {
                Long resourceId = resourceIds[resourceIndex++];
                String accountName = resourceInfoMapper.getAccountNameByUserId(userIds[i]);
                updateCount += resourceInfoMapper.clientShift(accountName, resourceId, userIds[i]);
            }
        }
        return updateCount;
    }

    //批量导出
    @Override
    public List<ResourceInfo> findAllByIds(List<Long> ids) {
        ResourceInfo resourceInfo = new ResourceInfo();
        List<ResourceInfo> resourceInfoList = resourceInfoMapper.findAllByResourceId(resourceInfo, ids);
        for (ResourceInfo client : resourceInfoList) {
            Integer isCall = client.getIsCall();
            if (isCall != null) {
                String isCallStr = isCall == 0 ? "否" : (isCall == 1 ? "是" : "");
                client.setIsCallStr(isCallStr);
            }
            Integer lifeCycle = client.getLifeCycle();
            if (lifeCycle != null) {
                String lifeCycleStr = "";
                switch (lifeCycle) {
                    case 0:
                        lifeCycleStr = "潜在";
                        break;
                    case 1:
                        lifeCycleStr = "签约";
                        break;
                    case 2:
                        lifeCycleStr = "重复购买";
                        break;
                    case 3:
                        lifeCycleStr = "失效";
                        break;
                    default:
                        lifeCycleStr = "";
                }
                client.setLifeCycleStr(lifeCycleStr);
                Integer resourceTag = client.getResourceTag();
                String resourceTagStr;
                switch (resourceTag) {
                    case 0:
                        resourceTagStr = "未标记";
                        break;
                    case 1:
                        resourceTagStr = "无效";
                        break;
                    case 2:
                        resourceTagStr = "待定";
                        break;
                    case 3:
                        resourceTagStr = "有效";
                        break;
                    default:
                        resourceTagStr = "";
                }
                client.setResourceTagStr(resourceTagStr);
            }
        }
        return resourceInfoList;
    }

    //查询电话是否存在
    @Override
    public Integer isExistPhone(String phoneA, String phoneB, String accountName) {
        Integer result = resourceInfoMapper.isExistPhoneA(accountName, phoneA);
        if (result > 0) {
            return result;
        }
        result = resourceInfoMapper.isExistPhoneB(accountName, phoneA);
        if (result > 0) {
            return result;
        }
        if (phoneB != null) {
            result = resourceInfoMapper.isExistPhoneA(accountName, phoneB);
            if (result > 0) {
                return result;
            }
            result = resourceInfoMapper.isExistPhoneB(accountName, phoneB);
        }
        return result;
    }

    //客户导入
    @Override
    public Integer importClient(List<ResourceInfo> resourceInfoList, String accountName) {
        List<ResourceInfo> resultresourceInfoList = new ArrayList<>();
        for (ResourceInfo resourceInfo : resourceInfoList) {
            Integer isExistPhone = this.isExistPhone(resourceInfo.getPhoneA(), resourceInfo.getPhoneB(), accountName);
            if (isExistPhone <= 0) {
                resourceInfo.setUserId(getAdminIdByAccountName(accountName));
                resourceInfo.setAccountName(accountName);
                resourceInfo.setCreateTime(Timestamp.from(Instant.now()).toString());
                resourceInfo.setResourceTag(3);
                resourceInfo.setIsExists(1);
                resourceInfo.setIsHighSeas(0);
                resultresourceInfoList.add(resourceInfo);
            }
        }
        return resourceInfoMapper.importClient(resultresourceInfoList);
    }

    @Override
    public Long getAdminIdByAccountName(String accountName) {
        return resourceInfoMapper.getAdminIdByAccountName(accountName);
    }
}
